package com.pig4cloud.pig.module.controller.product;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.mybatis.page.PageUtils;
import com.pig4cloud.pig.common.mybatis.page.TableDataInfo;
import com.pig4cloud.pig.module.api.entity.product.ProductRuleRelation;
import com.pig4cloud.pig.module.service.product.IProductRuleRelationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 规格和商家服务类型Controller
 *
 * @author ruoyi
 * @date 2022-02-06
 */
@Validated
@Api(value = "规格和商家服务类型控制器", tags = {"规格和商家服务类型管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/rule/relation")
public class ProductRuleRelationController {

    private final IProductRuleRelationService productRuleRelationService;

    /**
     * 查询规格和商家服务类型列表
     */
    @ApiOperation("查询规格和商家服务类型列表")
//    @PreAuthorize("@pms.hasPermission('system:relation:list')")
    @GetMapping("/list")
    public TableDataInfo<ProductRuleRelation> list(@Validated ProductRuleRelation productRuleRelation) {
        QueryWrapper<ProductRuleRelation> qw = new QueryWrapper<>();
        if(productRuleRelation.getCateChildId() != null){
            qw.eq("cate_child_id",productRuleRelation.getCateChildId());
        }
        TableDataInfo<ProductRuleRelation> td = PageUtils.buildDataInfo(productRuleRelationService.page(PageUtils.buildPage(),qw));
        List<ProductRuleRelation> list = td.getData();
        return td;
    }

    /**
     * 获取规格和商家服务类型详细信息
     */
    @ApiOperation("获取规格和商家服务类型详细信息")
//    @PreAuthorize("@pms.hasPermission('system:relation:query')")
    @GetMapping("/{id}")
    public R<ProductRuleRelation> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        ProductRuleRelation productRuleRelation = productRuleRelationService.getById(id);
        return R.ok(productRuleRelation);
    }

    /**
     * 新增规格和商家服务类型
     */
    @ApiOperation("新增规格和商家服务类型")
//    @PreAuthorize("@pms.hasPermission('system:relation:add')")
    @SysLog("规格和商家服务类型")

    @PostMapping()
    public R<Void> add(@Valid @RequestBody ProductRuleRelation productRuleRelation) {
        return R.okByRow(productRuleRelationService.save(productRuleRelation) ? 1 : 0);
    }

    /**
     * 修改规格和商家服务类型
     */
    @ApiOperation("修改规格和商家服务类型")
//    @PreAuthorize("@pms.hasPermission('system:relation:edit')")
    @SysLog("规格和商家服务类型")

    @PutMapping()
    public R<Void> edit(@Valid @RequestBody ProductRuleRelation productRuleRelation) {
        return R.okByRow(productRuleRelationService.updateById(productRuleRelation) ? 1 : 0);
    }

    /**
     * 删除规格和商家服务类型
     */
    @ApiOperation("删除规格和商家服务类型")
//    @PreAuthorize("@pms.hasPermission('system:relation:remove')")
    @SysLog("规格和商家服务类型")
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return R.okByRow(productRuleRelationService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
